<?php

class g5_user extends Model{

    public $is_use_cache = false;

    function __construct(){
        parent::__construct();
        $this->_tableName = 'user';
    }

    function crc($ucode = '', $mod = 10){
        $user_code = $ucode == '' ? $_SESSION['user']['user_code'] : $ucode;
        $this->_tableName = 'g5_user_'.crc($user_code, $mod);
        return $this;
    }

    function getOne($field = '*', $where = array(), $cachekey = null, $exp = null){
        $cachekey = $this->getCacheKey($where);
        return parent::getOne($field, $where, $cachekey, USER_GET_ONE_EXP_TIME);
    }

    function update($data, $where, $cachekey = null){
        $cachekey = $this->getCacheKey($where);
        $result = parent::update($data, $where);
        if($cachekey){
            $this->cache->del($cachekey);
        }
        return $result;
    }

    private function getCacheKey($where = array()){
        $cachekey = '';
        if($this->is_use_cache == false){
            return $cachekey;
        }elseif(!empty($_SESSION['user']['user_code'])){
            return $_SESSION['user']['user_code'].USER_GET_ONE;
        }elseif(!empty($where['user_code'])){
            return $where['user_code'].USER_GET_ONE;
        }
        return $cachekey;
    }

    public function getTotal($where=array()){
    	$total = $this->getCount('*',$where);
    	return $total;
    }

    public function getList($page, $pagesize, $where){
    	$filed='user.*';
    	$total = $this->limit($pagesize, ($page - 1)*$pagesize)->by('`user`.`user_id`')->getAll($filed, $where);
    	return $total;
    }
    /**
     * 寰楀埌鎵�湪琛�     */
    public function userTable($userId){
        $hash 		= $userId % 64;
        $tableName 	=  'user'.$hash;
        return $tableName;
    }
}